Documentation > Developers > Custom Language Switcher

Custom Language Switcher

Browse Docs
Search Documentation
If you wish to design a custom TranslatePress language switcher, there is a function you can implement to help you with all the necessary information when using the plugin.

Create a TranslatePress Custom Language Switcher

The function that needs to be called is trp_custom_language_switcher().
The function returns an array with the following parameters:
  • $custom_ls_array[$language_code]['language_name'] – the full language name
  • $custom_ls_array[$language_code]['language_code'] – the language code of the respective language
  • $custom_ls_array[$language_code]['short_language_name'] –  the language code of the language, the slug which will be used in the url
  • $custom_ls_array[$language_code]['flag_link'] – a link to the flag of the respective language
  • $custom_ls_array[$language_code]['current_page_url'] – the URL of the current language with the language slug added

 

Here is an example of a custom-made language switcher:

TranslatePress custom language switcher

While here you can see the code snippet:

<?php $array = trp_custom_language_switcher(); ?>
<!-- IMPORTANT! You need to have data-no-translation on the wrapper with the links or TranslatePress will automatically translate them in a secondary language. -->
<ul data-no-translation>
<!-- // Check whether TranslatePress can run on the current path or not. If the path is excluded from translation, trp_allow_tp_to_run will be false -->
<?php if ( apply_filters( 'trp_allow_tp_to_run', true ) ){ ?>
<?php foreach ($array as $name => $item){ ?>
<li style="list-style-image: url(<?php echo $item['flag_link'] ?>)">
<a href="<?php echo $item['current_page_url']?>">
<span><?php echo $item['short_language_name']. ':' . $item['language_name']?>
</span>
</a>
</li>
<?php } ?>
<?php } ?>
</ul>

Note: You need to have data-no-translation on the wrapper with the links or TranslatePress will automatically translate them in a secondary language.​

You can also create a custom language switcher shortcode that you can then use like [custom-language-switcher]

/*
* Custom language switcher shortcode
*/
add_shortcode('custom-language-switcher', 'trpc_custom_language_switcher', 10);
function trpc_custom_language_switcher(){
// Check whether TranslatePress can run on the current path or not. If the path is excluded from translation, trp_allow_tp_to_run will be false
if ( apply_filters( 'trp_allow_tp_to_run', true ) ){
$languages = trp_custom_language_switcher();
$html = "<ul data-no-translation>";
foreach ($languages as $name => $item) {
$html .= "<li style='list-style-image: url({$item['flag_link']})'>";
$html .= "<a href='{$item['current_page_url']}'>";
$html .= "<span>{$item['language_name']}</span></a></li>";
}
$html .= "</ul>";
return $html;
}
}

If you just need to quickly create a classic language switcher, check out TranslatePress’ default language switcher settings.

TranslatePress Multilingual

The best website localization service for WordPress websites. Manage everything from a visual interface.